<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@include file="connection.jsp"%>
<% 
	String userid = (String) session.getAttribute("userid");

    if (userid == null)
    {
       response.sendRedirect("login.jsp");
    }  
    
	String pId = request.getParameter("pid");
    Connection c = getConnection(session);
    Statement s = c.createStatement();
    Statement s2 = c.createStatement();
	String docCode="";
	
	Connection cadmin = getAdminConnection();
	Statement sadmin = cadmin.createStatement();
	
	ResultSet rs = sadmin.executeQuery("select Doc_Code from users where UserID= '"+userid+"'");	
	if(rs.first()){
		docCode = rs.getString(1);
	}	
%>
<%!
public boolean exists(String diagCode, String docCode, Statement s )throws SQLException, ClassNotFoundException{
	ResultSet rs = s.executeQuery("select ProcCode from tblsuperbilltemplate_proc where DocCode = '"+docCode+"' and ProcCode= '"+diagCode+"'");
	boolean exist = false;
	if(rs.first())
		exist = true;
		
	rs.close();	
	
	return exist;		
}
%>
<html>
    <head>
	<link href="css/medbill.css" rel="stylesheet" type="text/css" />
    <title>PMS</title>
    <script language="javascript">
    function validateForm(){
    
	var procCounter=0;
		for(i=0 ; i<document.patient_diag.cpt_code.length; i++)
		{		
			if(document.patient_diag.cpt_code[i].checked){
				selectedProc="true";
				procCounter++;
			}			
		}
		
		if(procCounter==0){
			alert("Please select at least one item.");
			return false;
		}
		return true;
	}
    </script>
    </head>
    <body>
	<table width="860" height="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="204" valign="top"><%@include file="header.jsp"%></td>
  </tr>
  <tr>
    <td height="30" bgcolor="#1f226f" class="headingBlue">Medical Billing and Claim Processing Professionals</td>
  </tr>
  <tr>
    <td valign="top" bgcolor="#ccae74"><table width="96%" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="470" valign="top" class="txtPageTitle">Procedure Template</td>
            <td>&nbsp;</td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="560"></td>
              </tr>
              <tr>
                <td class="txtHomeBody"></td>
              </tr>
            </table>
              <br>
        <form method="post" name="patient_diag" action="SaveTemplate.jsp" onsubmit="return validateForm();">
        <input type="hidden" name="doc_code" value="<%=docCode%>"/>
         </div>
            <font color="red"><%if(request.getParameter("saved")!=null){out.print("Template saved succussfully.");}%></font>
            <br>
            <table border="0" width="100%" style="border-collapse: collapse">
                <tr>
                    <th width="24">&nbsp;</th>
                    <th width="38">CPT</th>
                    <th>Procedures</th>
                    <th width="24">&nbsp;</th>
                    <th width="38">CPT</th>
                    <th>Procedures</th>

                </tr>
                <%
            rs = s.executeQuery("select * from tblprocedure where active='Yes' order by description");
            while (rs.next()) {
                %>
                <tr>
                    <td width="24">
                    <%if(exists(rs.getString("code"), docCode, s2)){%><input type="checkbox" checked name="cpt_code" value="<%=rs.getString("code")%>" />
                    <%}else{%>
	                    <input type="checkbox" name="cpt_code" value="<%=rs.getString("code")%>" />
					<%}%>
                    </td>
                    <td width="38"><%=rs.getString("CPT")%></td>
                    <td><%=rs.getString("Description")%></td>                    
                    <%
                    if(rs.next()){
                    %>
                    <td width="24"><%if(exists(rs.getString("code"), docCode, s2)){%><input type="checkbox" checked name="cpt_code" value="<%=rs.getString("code")%>" />
                    <%}else{%>
	                    <input type="checkbox" name="cpt_code" value="<%=rs.getString("code")%>" />
					<%}%>
					</td>
                    <td width="38"><%=rs.getString("CPT")%></td>
                    <td><%=rs.getString("Description")%></td>
                    <%
                    }else{
                    %>
	                  <td width="24">&nbsp;</td>
                   	  <td width="38">&nbsp;</td>
                   	  <td>&nbsp;</td>
                    <%
                    }
                    %>
                </tr>
                <%
            }
                %>
            </table>         
            <p><b>
            <br>
            <input type="submit" value="Save" style="width:100px;"/> <input type="button" value="Cancel" style="width:100px;" onclick="location='home.jsp'"/>
        </p>
        </form>
		  <br></td>
            <td width="45" valign="top">&nbsp;</td>
            <td valign="top">&nbsp;</td>
          </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="62" valign="top"><!-- #BeginLibraryItem "/Library/footer.lbi" --><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="4" bgcolor="#c7540f"></td>
      </tr>
      <tr>
        <td height="58" bgcolor="#1e2667">&nbsp;</td>
      </tr>
    </table><!-- #EndLibraryItem --></td>
  </tr>
</table>
    </body>
</html>
<%
            rs.close();
            s.close();
			sadmin.close();
            s2.close();
            c.close();
			cadmin.close();
%>